//
//  JEHMineFirstTableViewCell.m
//  SinaNews
//
//  Created by 蒋恩浩 on 15/12/9.
//  Copyright © 2015年 JEH. All rights reserved.
//

#import "JEHMineFirstTableViewCell.h"

@interface JEHMineFirstTableViewCell ()

@property (nonatomic, retain) UIImageView *loginPic;

@end

@implementation JEHMineFirstTableViewCell

- (void)dealloc {
    
    [_userLabel release];
    [_userPicture release];
    _delegate = nil;
    [_loginPic release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
        self.backgroundColor = [UIColor grayColor];
    }
    return self;
}

- (void)createView {
    
    NSArray *buttonPic = @[@"collect", @"comment", @"gift"];
    NSArray *labelText = @[@"收藏", @"反馈", @"研发中心"];
    self.loginPic = [[UIImageView alloc] initWithFrame:CGRectZero];
    [_loginPic release];
    _loginPic.backgroundColor = [UIColor cyanColor];
    _loginPic.alpha = 0.7;
    [self addSubview:_loginPic];
    
    self.userLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [_userLabel release];
//    _userLabel.text = @"登陆账号";
    _userLabel.textColor = [UIColor whiteColor];
    _userLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_userLabel];
    self.userPicture = [[UIImageView alloc] initWithFrame:CGRectZero];
    [_userPicture release];
//    _userPicture.image = [UIImage imageNamed:@"userPicture"];
    _userPicture.layer.masksToBounds = YES;
    _userPicture.layer.cornerRadius = 40;
    [self addSubview:_userPicture];
    
    for (int i = 0; i < 3; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = 100 + i;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.tag = 200 + i;
        label.text = labelText[i];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:12];
        UIImageView *picture = [[UIImageView alloc] initWithFrame:CGRectZero];
        picture.image = [UIImage imageNamed:buttonPic[i]];
        picture.backgroundColor = [UIColor whiteColor];
        picture.contentMode = UIViewContentModeScaleAspectFit;
        picture.clipsToBounds = YES;
        picture.tag = 300 + i;
        
        [self addSubview:picture];
        [self addSubview:label];
        [self addSubview:button];
    }
}

- (void)dayAndNight {
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    if ([[user objectForKey:@"NightMode"] isEqualToString:@"Night"]) {
        
        for (int i = 0; i < 3; i++) {
            
            UIImageView *picture = (UIImageView *)[self viewWithTag:300 + i];
            picture.backgroundColor = [UIColor grayColor];
            UILabel *label = (UILabel *)[self viewWithTag:200 + i];
            label.backgroundColor = [UIColor grayColor];
            label.textColor = [UIColor whiteColor];
        }
    } else {
        for (int i = 0; i < 3; i++) {
            
            UIImageView *picture = (UIImageView *)[self viewWithTag:300 + i];
            picture.backgroundColor = [UIColor whiteColor];
            UILabel *label = (UILabel *)[self viewWithTag:200 + i];
            label.backgroundColor = [UIColor whiteColor];
            label.textColor = [UIColor blackColor];
        }
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.loginPic.frame = CGRectMake(0, 0, self.width, self.height - 50);
    self.userPicture.frame = CGRectMake(self.width / 2 - 40, 20, 80, 80);
    self.userLabel.frame = CGRectMake(0, 100, self.width, 30);
    for (int i = 0; i < 3; i++) {
        
        UIButton *button = (UIButton *)[self viewWithTag:100 + i];
        UILabel *label = (UILabel *)[self viewWithTag:200 + i];
        UIImageView *picture = (UIImageView *)[self viewWithTag:300 + i];
        button.frame = CGRectMake(i * self.width / 3, self.height - 50, self.width / 3, 50);
        label.frame = CGRectMake(i * self.width / 3, self.height - 20, self.width / 3 - 1, 20);
        picture.frame = CGRectMake(i * self.width / 3, self.height - 50, self.width / 3 - 1, 30);
    }
    [self dayAndNight];
}

- (void)buttonAction:(UIButton *)sender {
    
    [self.delegate passButtonTag:sender.tag];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
